//
//  XGResetPasswordViewController.m
//  Instagram
//
//  Created by monkey on 2019/4/13.
//  Copyright © 2019 itcast. All rights reserved.
//

#import <SVProgressHUD/SVProgressHUD.h>

#import "XGResetPasswordViewController.h"

@interface XGResetPasswordViewController ()

/// 邮箱
@property (nonatomic,strong) UITextField *emailTextField;
/// 重置密码按钮
@property (nonatomic,strong) UIButton *resetPasswordButton;
/// 取消按钮
@property (nonatomic,strong) UIButton *cancelButton;


@end

@implementation XGResetPasswordViewController

#pragma mark - 控制器生命周期方法

- (void)loadView
{
    [super loadView];
    
    [self setUpUI];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // 添加手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchViewForHiddenKeyboardAction)];
    [self.view addGestureRecognizer:tap];
}

#pragma mark - 事件监听

/// 重置密码监听事件
- (void)resetPasswordAction
{
    // 退出键盘
    [self.view endEditing:YES];
    
    // 判断字段是否为空
    if (!_emailTextField.hasText) {
        [SVProgressHUD showErrorWithStatus:@"邮箱不能为空"];
        return;
    }
    
    // 验证邮箱格式
    if (!_emailTextField.text.isEmail) {
        [SVProgressHUD showErrorWithStatus:@"邮箱格式不正确!请检查后重新填写"];
        return;
    }
    
    // 重置密码
    [XGAccountViewModel resetPasswordWithEmail:_emailTextField.text completion:^(BOOL isSuccess, NSString * _Nonnull message) {
        if (isSuccess) {
            [SVProgressHUD showSuccessWithStatus:message];
        } else {
            [SVProgressHUD showErrorWithStatus:message];
        }
        
        [self goBackAction];
    }];
}

/// 退出重置密码页面监听事件
- (void)goBackAction
{
    [self.view endEditing:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}

/// 隐藏键盘事件
- (void)touchViewForHiddenKeyboardAction
{
    [self.view endEditing:YES];
}

#pragma mark - 其他方法

/// 设置界面
- (void)setUpUI
{
    self.view.backgroundColor = [UIColor red:210 green:254 blue:91 alpha:1];
    
    // 添加子控件
    [self.view addSubview:self.emailTextField];
    [self.view addSubview:self.resetPasswordButton];
    [self.view addSubview:self.cancelButton];
    
    // 设置位置
    _emailTextField.frame = CGRectMake(20, 150, self.view.width - 40, 36);
    _resetPasswordButton.frame = CGRectMake(20, _emailTextField.maxY + 30, 80, 36);
    _cancelButton.frame = CGRectMake(self.view.width - 80 - 20, _resetPasswordButton.y, 80, 36);
}

#pragma mark - 懒加载

- (UITextField *)emailTextField
{
    if (!_emailTextField) {
        _emailTextField = [UITextField textFieldWithPlaceholder:@"邮箱地址(必填)" fontSize:15 textColor:[UIColor darkTextColor] isSecureTextEntry:NO keyboardType:UIKeyboardTypeEmailAddress];
    }
    
    return _emailTextField;
}

- (UIButton *)resetPasswordButton
{
    if (!_resetPasswordButton) {
        _resetPasswordButton = [UIButton buttonWithTitle:@"重置密码" fontSize:15 titleColor:[UIColor whiteColor] target:self action:@selector(resetPasswordAction)];
        _resetPasswordButton.backgroundColor = [UIColor red:245 green:90 blue:93 alpha:1];
    }
    
    return _resetPasswordButton;
}

- (UIButton *)cancelButton
{
    if (!_cancelButton) {
        _cancelButton = [UIButton buttonWithTitle:@"取消" fontSize:15 titleColor:[UIColor whiteColor] target:self action:@selector(goBackAction)];
        _cancelButton.backgroundColor = [UIColor orangeColor];
    }
    
    return _cancelButton;
}


@end
